package cn.rokhdelar.ams.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.rokhdelar.ams.management.domain.po.SysDictionary;
import cn.rokhdelar.ams.management.domain.po.SysDictionaryItem;
import cn.rokhdelar.ams.management.domain.vo.DictionaryItemVO;
import cn.rokhdelar.ams.management.mapper.SysDictionaryItemMapper;
import cn.rokhdelar.ams.management.mapper.SysDictionaryMapper;
import cn.rokhdelar.ams.management.service.SysDictionaryItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author Rokhdelar
 * @since 2024/10/21 -- 16:57
 **/

@Service
@RequiredArgsConstructor
public class SysDictionaryItemServiceImpl extends ServiceImpl<SysDictionaryItemMapper, SysDictionaryItem>
        implements SysDictionaryItemService {
    private final SysDictionaryMapper sysDictionaryMapper;

    /**
     * 获取指定字典的所有字典项列表
     * @param dictionaryId 字典ID
     * @return 字典所含字典项的列表
     */
    @Override
    @Transactional
    public List<DictionaryItemVO> getDictionaryItemList(String dictionaryId) {
        List<SysDictionaryItem> list = lambdaQuery()
                .eq(SysDictionaryItem::getDictionaryId, dictionaryId)
                .orderByAsc(SysDictionaryItem::getSort)
                .list();
        return BeanUtil.copyToList(list, DictionaryItemVO.class);
    }

    /**
     * 获取指定字典项的信息
     * @param itemId 字典项ID
     * @return 字典项实体VO
     */
    @Override
    @Transactional
    public DictionaryItemVO getDictionaryItemById(String itemId) {
        return BeanUtil.copyProperties(getById(itemId), DictionaryItemVO.class);
    }

    /**
     * 为指定的字典添加一个新的字典项
     * @param dictionaryId 字典ID
     * @param dictionaryItemVO 字典项VO实体
     * @return 是否成功
     */
    @Override
    @Transactional
    public Boolean addDictionaryItem(String dictionaryId, DictionaryItemVO dictionaryItemVO) {
        if (dictionaryItemVO.getDictionaryId() != null && !Objects.equals(dictionaryItemVO.getDictionaryId(), dictionaryId)) {
            throw new RuntimeException("参数错误，字典ID与字典项的字典ID不匹配！");
        } else if (dictionaryItemVO.getDictionaryId() == null) {
            dictionaryItemVO.setDictionaryId(dictionaryId);
        }

        return save(BeanUtil.copyProperties(dictionaryItemVO, SysDictionaryItem.class));
    }

    /**
     * 更新指定字典项的信息
     * @param dictionaryId 指定字典ID
     * @param dictionaryItemVO 字典项VO实体
     * @return 是否成功
     */
    @Override
    @Transactional
    public Boolean updateDictionaryItemById(String dictionaryId, DictionaryItemVO dictionaryItemVO) {
        return updateById(BeanUtil.copyProperties(dictionaryItemVO, SysDictionaryItem.class));
    }

    /**
     * 删除指定的字典项
     * @param itemId 字典项ID
     * @return 删除是否成功
     */
    @Override
    @Transactional
    public Boolean deleteDictionaryItemById(String itemId) {
        return removeById(itemId);
    }

    /**
     * @param key 字典键值
     * @return 字典项列表
     */
    @Override
    @Transactional
    public List<DictionaryItemVO> getDictionaryItemListByKey(String key) {
        LambdaQueryWrapper<SysDictionary> wrapper = new LambdaQueryWrapper<SysDictionary>().eq(SysDictionary::getDictionaryKey, key);
        SysDictionary dictionary = sysDictionaryMapper.selectOne(wrapper);
        if (!Objects.isNull(dictionary)) {
            List<SysDictionaryItem> list = lambdaQuery().eq(SysDictionaryItem::getDictionaryId, dictionary.getId()).list();
            return BeanUtil.copyToList(list, DictionaryItemVO.class);
        }
        return null;
    }

    @Override
    public DictionaryItemVO getDictionaryItemListByKeyAndStatus(String key, String status) {
        LambdaQueryWrapper<SysDictionary> wrapper = new LambdaQueryWrapper<SysDictionary>().eq(SysDictionary::getDictionaryKey, key);
        SysDictionary dictionary = sysDictionaryMapper.selectOne(wrapper);
        if (!Objects.isNull(dictionary)) {
            List<SysDictionaryItem> list = lambdaQuery().eq(SysDictionaryItem::getDictionaryId, dictionary.getId()).list();
            for (SysDictionaryItem item : list) {
                if (item.getValue().equals(status)) {

                    return BeanUtil.copyProperties(item, DictionaryItemVO.class);
                }
            }
        }
        return null;
    }
}
